package com.xxxx.server.utils.Gitee;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
public class GiteeUtil {

    @Value("${gitee.giteeAccessToken}")
    private String giteeAccessToken;

    @Value("${gitee.giteeOwner}")
    private String giteeOwner;

    @Value("${gitee.giteeRepoName}")
    private String giteeRepoName;

    private GiteeImgBed giteeImgBed;

    public GiteeUtil() {
        this.giteeImgBed = new GiteeImgBed(giteeAccessToken, giteeOwner, giteeRepoName);
    }
    public GiteeUtil(String giteeAccessToken,String giteeOwner,String giteeRepoName) {
        this.giteeImgBed = new GiteeImgBed(giteeAccessToken, giteeOwner, giteeRepoName);
    }

    /**
     * 添加文件到 Gitee
     * @param path 路径
     * @param file 文件
     * @return JSONObject
     * */
    public JSONObject addFile(String path, MultipartFile file) throws IOException {
        if (path == null){
            path = "";
        }
        //获取文件后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //设置图片名字  当前时间+唯一标识码+文件后缀
        String fileName = System.currentTimeMillis() + "_" + UUID.randomUUID().toString() + suffix;
        String paramImgFile = Base64.encode(file.getBytes());
        //设置转存到Gitee仓库参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", giteeImgBed.ACCESS_TOKEN);
        paramMap.put("message", giteeImgBed.ADD_MESSAGE);
        paramMap.put("content", paramImgFile);
        //转存文件路径
        String targetDir = giteeImgBed.PATH + path + "/" + fileName;
        //设置请求路径
        String requestUrl = String.format(GiteeImgBed.CREATE_REPOS_URL, giteeImgBed.OWNER,
                giteeImgBed.REPO_NAME, targetDir);
        log.info("请求Gitee仓库路径:{}",requestUrl);
        String resultJson = HttpUtil.post(requestUrl, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(resultJson);
        return jsonObject;
    }

    /**
     * 获取仓库具体路径下的内容
     * @param imgPath 文件路径
     * @return JSONObject
     */
    public JSONObject getSpecificPath(String imgPath){
        String path = imgPath.split("master/")[1];
        //设置请求路径
        String requestUrl = String.format(GiteeImgBed.GET_IMG_URL, giteeImgBed.OWNER,
                giteeImgBed.REPO_NAME, path);
        log.info("请求Gitee仓库路径:{}",requestUrl);
        //获取图片所有信息
        String resultJson = HttpUtil.get(requestUrl);
        JSONObject jsonObject = JSONUtil.parseObj(resultJson);
        return jsonObject;
    }

    /**
     * 删除文件
     * @param sha 文件的 Blob SHA，可通过 [获取仓库具体路径下的内容] API 获取
     * @param imgPath 文件路径
     * @return JSONObject
     */
    public JSONObject delFile(String sha,String imgPath){
        String path = imgPath.split("master/")[1];

        //设置删除请求参数
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("access_token", giteeImgBed.ACCESS_TOKEN);
        paramMap.put("sha", sha);
        paramMap.put("message", giteeImgBed.DEl_MESSAGE);
        //设置删除路径
        String requestUrl = String.format(GiteeImgBed.DEL_IMG_URL, giteeImgBed.OWNER,
                giteeImgBed.REPO_NAME, path);
        log.info("请求Gitee仓库路径:{}",requestUrl);
        //删除文件请求路径
        String resultJson = HttpRequest.delete(requestUrl).form(paramMap).execute().body();
        HttpRequest.put(requestUrl).form(paramMap).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(resultJson);
        return jsonObject;
    }

}
